package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.ValleyBean;
import com.emip.dao.ValleyDAO;
import com.emip.service.ValleyService;

@Service("valleyService")
public class ValleyServiceImpl implements ValleyService {

	@Autowired
	ValleyDAO valleyDAO;

	public Boolean deleteValley(int id) {
		ValleyBean delBean = valleyDAO.findByID(id);
		valleyDAO.remove(delBean);
		return true;
	}

	public List<ValleyBean> getAll() {
		return valleyDAO.getAll();
	}

	public ValleyBean getValleyByID(int id) {
		return valleyDAO.findByID(id);
	}

	public ValleyBean addValley(String name, String waterSystem, String alongSide) {
		ValleyBean newBean = new ValleyBean();
		newBean.setName(name);
		newBean.setWaterSystem(waterSystem);
		newBean.setAlongSide(alongSide);
		//查重，是否已经有相同的name, waterSystem, alongSide的记录存在
		//若有重复记录，则返回null
		if (valleyDAO.findExist(newBean)) {
			return null;
		}
		int newID = valleyDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
	}

	public ValleyBean updateValley(int id, String name, String waterSystem, String alongSide) {
		ValleyBean newBean = valleyDAO.findByID(id);
		newBean.setName(name);
		newBean.setWaterSystem(waterSystem);
		newBean.setAlongSide(alongSide);
		//查重，是否已经有相同的name, waterSystem, alongSide的记录存在
		//若有重复记录，则返回null
		if (valleyDAO.findExist(newBean)) {
			return null;
		}
		valleyDAO.update(newBean);
		return newBean;
	}

}
